package com.im.task;

import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.im.service.TextMessageService;
import com.im.util.Config;
import com.im.util.IMManager;
/**
 * 
 * @Description: TODO(设定定时任务，根据配置进行聊天记录的清除) 	
 * @date 2017年3月9日 下午9:55:15 
 * @author 岸芷汀翎
 */
@Component
public class ScheduledTasks {
	@Autowired
	private TextMessageService messageService;
	@Autowired 
	private Config config;
	
	@Scheduled(cron="0 0 2 * * ?")
    public void cleanTask() {
        if(config.getRemoveCashHour() != null && config.getRemoveCashHour() > 0){
        	IMManager.cleanCashData(config.getRemoveCashHour());
        }
        this.cleanMessage();
    }
	
	private void cleanMessage(){
		if(config.getRemoveDataDay() != null && config.getRemoveDataDay() > 0){
			Date time =DateUtils.addDays(new Date(), -config.getRemoveDataDay());
			messageService.cleanDataBeginTime(time);
		}
	}
	
}
